// pages/xxsf/xxsf.js
var times = require('../../utils/times.js')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        show: false,
        triggered: false,
        show2: false,
        onClose: false,
        inputValue: '',
        comment_id: '',
        show3: true,
        message:'',
        userInfo:'',
    },
    beiwanglu(e) {
        this.setData({
            show: true
        })
    },
    onClose(e) {
        this.setData({ show2: false, inputValue: '' });
    },
    //评论
    comment(e) {
        console.log(e.currentTarget.id)
        this.setData({
            show2: true,
            comment_id: e.currentTarget.id
        })
    },
    //跳转到登录页面
    login(e) {
        wx.navigateTo({
            url: '../register/register',
        })
    },
    //发布评论
    post_comment(e) {
        console.log(e.currentTarget.id)
        let that = this
        wx.cloud.database().collection('dynamic').doc(that.data.comment_id).get({
            success(res) {
                console.log(res)
                var action = res.data
                var comment = {
                    nickName: that.data.user.nickName,
                    text: that.data.inputValue,
                    tonickName: '',
                }
                console.log(comment, '33333')
                action.commentList.push(comment)
                console.log(action.commentList, '333333')
                wx.cloud.database().collection('dynamic').doc(that.data.plid).update({
                    data: {
                        commentList: action.commentList
                    },
                    success(res) {
                        wx.showToast({
                            title: '评论成功!',
                        })
                    }
                })
                that.setData({ show2: false, inputValue: '' })
            }
        })
    },
    getValue(e) {
        console.log(e.detail.value)
        this.setData({
            inputValue: e.detail.value
        })
    },
    // dz(e) {
    //     console.log(e.currentTarget.id)
    //     let that = this
    //     wx.cloud.database().collection('dynamic').doc(e.currentTarget.id).get({
    //         success(res) {
    //             console.log(res)
    //             var action = res.data._id
    //             if (e.currentTarget.id == action) {
    //                 wx.cloud.database().collection('dynamic').doc(e.currentTarget.id).update({
    //                     data: {
    //                         dianzan: 1
    //                     },
    //                 })
    //             }
    //         }
    //     })
    // },
    // qx(e) {
    //     console.log(e.currentTarget.id)
    //     let that = this
    //     wx.cloud.database().collection('dynamic').doc(e.currentTarget.id).get({
    //         success(res) {
    //             console.log(res)
    //             var action = res.data._id
    //             if (e.currentTarget.id == action) {
    //                 wx.cloud.database().collection('dynamic').doc(e.currentTarget.id).update({
    //                     data: {
    //                         dianzan: 0
    //                     },
    //                 })
    //             }
    //         }
    //     })
    // },
    memorandum(e){
      console.log(e)
      let that=this
      wx.cloud.database().collection('memorandum').add({
          data:{
              nickName:that.data.user.nickName,
              _createTime:Date.now(),
              Record_text:that.data.message
          },
          success(res){
              wx.showToast({
                title: '我记住了！',
              })
          }
      })
    },
    getValue1(e){
        console.log(e.detail.value)
        this.setData({
            message:e.detail.value
        })
      },
    /**
     * 生命周期函数--监听页面加载
     */
    //获取用户信息
    getuserList() {
        let that = this
        let user = wx.getStorageSync('user')
        let openid = user._openid
        wx.cloud.database().collection("user").where({ _openid: openid }).get({
            success(res) {
                console.log(res.data, '1333')
                that.setData({ userInfo: res.data })
            }
        })
    },
    //获取动态信息
    getactionList() {
        var that = this
        wx.cloud.database().collection('dynamic').orderBy('time', 'desc').get({
            success(res) {
                for (var i = 0; i < res.data.length; i++) {
                    res.data[i]["time"] = times.toDate(res.data[i]["time"])
                }
                that.setData({
                    actionsList: res.data
                })
            }
        })
    },
    onLoad(options) {
        this.getactionList()
        this.getuserList()
    },
    //删除动态
    deleteAction(e) {
        console.log(e.currentTarget.id)
        wx.cloud.database().collection('dynamic').doc(e.currentTarget.id).remove({
            success(res) {
                console.log(res)
                wx.showToast({
                    title: '删除成功',
                })
            }
        })
    },
    //下拉刷新
    refresherrefresh() {
        this.getactionList()
        setTimeout(() => {
            this.setData({ triggered: false })
        }, 700);
    },
    //跳转到发布页面
    Release(e) {
        wx.navigateTo({
            url: '../Dynamic_publish/Dynamic_publish',
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        let user = wx.getStorageSync('user')
        console.log('缓存的user', user)
        this.setData({
            user: user,
        })
        this.getuserList()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})